package com.cgbl.cg.vm.work

import android.app.Application
import android.os.Bundle
import android.text.TextUtils
import androidx.databinding.ObservableField
import api.UserServiceFactory
import api.WorkServiceFactory
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.configs.AppConstants
import com.robot.base.util.ToastUtils
import com.robot.base.util.arouter.ARouterUtil
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.binding.command.BindingConsumer
import me.goldze.mvvmhabit.http.BaseResponse
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

class ReleaseWorkViewModel(application: Application) : GLBaseViewModel(application) {
    val name = ObservableField<String>()
    val nameEditSize = ObservableField<String>("0/50")

    val direction = ObservableField<String>()
    val directionEditSize = ObservableField<String>("0/200")

    val imgList = ArrayList<String>()
    var copyrightType = ""
    var imgUrl = ""
    var save = false
    var declare = ""
    var type = ""
    var tagList = arrayListOf<String>()

    val nameCommand = BindingCommand<String>(BindingConsumer {
        nameEditSize.set("${it.length}/50")
    })

    val directionCommand = BindingCommand<String>(BindingConsumer {
        directionEditSize.set("${it.length}/200")
    })

    val confirmCommand = BindingCommand<Any>(BindingAction {
        if (TextUtils.isEmpty(name.get())) {
            ToastUtils.showShort("作品标题不能为空")
            return@BindingAction
        }

        if (TextUtils.isEmpty(direction.get())) {
            ToastUtils.showShort("作品说明不能为空")
            return@BindingAction
        }
        if (imgList.isNullOrEmpty()) {
            ToastUtils.showShort("请选择图片")
            return@BindingAction
        }
        ARouterUtil.goToActivityWithBundle(AppConstants.Router.WORK.A_NEXT_RELEASE_WORK,
            Bundle().apply {
                putString("title", name.get())
                putString("direction", direction.get())
                putStringArrayList("list", imgList)
            }
        )
    })

    fun doUpload(
        fileName: String,
        path: String,
        success: (uploadBean: String) -> Unit
    ) {
        val fileRQ: RequestBody = RequestBody.create(
            "multipart/form-data".toMediaTypeOrNull(),
            File(path)
        )
        val part = MultipartBody.Part.createFormData("file", fileName, fileRQ)
//        showLoadingDialog()
        UserServiceFactory.upload(part)
            .subscribe(object : AbstractViewModelSubscriber<BaseResponse<String>>(this) {
                override fun onNext(t: BaseResponse<String>) {
//                    dismissLoadingDialog()
                    ToastUtils.showShort("上传成功")
                    success.invoke(t.data)
                    imgList.add(t.data)
                }
            })
    }

    fun createWork() {
        val map = hashMapOf<String, Any>(
            "contentImages" to imgList,
            "coverImages" to (imgUrl),
            "isSave" to if (save) {
                1
            } else {
                0
            },
            "tags" to tagList,
            "type" to 0,
            "workExplain" to (direction.get() ?: ""),
            "workName" to (name.get() ?: "")
        )
        if (!TextUtils.isEmpty(declare))
            map["creationDescType"] = declare.toInt()

        if (!TextUtils.isEmpty(copyrightType))
            map["copyrightType"] = copyrightType.toInt()

        if (!TextUtils.isEmpty(type))
            map["workTypeId"] = type.toInt()

        showLoadingDialog()
        WorkServiceFactory.createWorks(map)
            .subscribe(object : AbstractViewModelSubscriber<BaseResponse<String>>(this) {
                override fun onNext(t: BaseResponse<String>) {
                    finishCommand.execute()
//                    ToastUtils.showShort("新建成功")
                }
            })
    }
}